/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
 ***/
package stripesbook.action.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.stripesbook.rayures.view.ViewTemplate;
import org.stripesbook.rayures.view.stripes.SFormat;
import org.stripesbook.rayures.view.stripes.SLabel;
import org.stripesbook.rayures.view.util.Formatter;
import stripesbook.model.Contact;

public class ContactDetails extends ViewTemplate<ContactDetails> {
  private Contact contact;

  public ContactDetails contact(Contact contact) {
    this.contact = contact;
    return getThis();
  }

  public List<Map<String, Object>> getFields() {
    List<Map<String, Object>> fields = new ArrayList<Map<String, Object>>();
    Formatter fmt = new Formatter(getViewContext().getLocale());

    fields.add(getField("contact.firstName", fmt.html(contact.getFirstName())));
    fields.add(getField("contact.lastName", fmt.html(contact.getLastName())));
    fields.add(getField("contact.email", contact.getEmail()));

    fields.add(getField("contact.phoneNumber",
      tag(SFormat.class).formatType("dashes").value(contact.getPhoneNumber())));

    fields.add(getField("contact.birthDate",
      fmt.date(contact.getBirthDate(), "yyyy-MM-dd")));

    fields.add(getField("contact.gender", bundle.get(contact.getGender().toString())));

    return fields;
  }

  private Map<String, Object> getField(String fieldId, Object value) {
    Map<String, Object> map = new HashMap<String, Object>();
    
    map.put("label", tag(SLabel.class).forId(fieldId));
    map.put("value", value);
    
    return map;
  }
}
